package com.song.JavaThread_ch3_8;

import java.util.List;
import java.util.concurrent.Exchanger;

public class Consumer implements Runnable{

	private List<String> buffer;
	
	private final Exchanger<List<String>> exchanger;
	
	
	
	public Consumer(List<String> buffer, Exchanger<List<String>> exchanger) {
		super();
		this.buffer = buffer;
		this.exchanger = exchanger;
	}



	@Override
	public void run() {
		int cycle = 1;
		
		for(int i = 0 ;i<10 ;i++){
			
			System.out.printf(" Consumer :cycle %d \n" ,cycle);
			
			try {
				buffer = exchanger.exchange(buffer);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.printf("Consumer : buffer size is :%d \n" , buffer.size());
			for( int j  =0 ;j <10 ;j++)
			{
				String msg = buffer.get(0);
				System.out.println(" Consumer :"+msg);
				buffer.remove(0);
			}
			cycle++;
		}
		
	}

}
